package blocos;

import java.util.Hashtable;

public class BlockWire extends BlockBit
{

	/* 
	 * Types of Wires
	 * 0 - horizontal
	 * 1 - vertical
	 * 2 - right/top
	 * 3 - right/bottom
	 * 4 - left/top
	 * 5 - left/bottom
	 */
	int type;
	
	public BlockWire(String v, int type) {
		super(v);
		this.type = type;
	}
	
	void attachRight (Block a ) throws DiferentsBlocks 
	{
		if((type == 0 )||(type == 2)||(type == 3))
		{	
			if( a instanceof blocos.BlockWire )
			{
				if( 
						(((BlockWire)a).type != 0 ) && 
						(((BlockWire)a).type != 4 ) && 
						(((BlockWire)a).type != 5 )
					)
				{
					throw new DiferentsBlocks();
				}
			}
			if( a instanceof blocos.BlockLogic)
			{
				this.right = a;
			}
			else if (a instanceof blocos.BlockBit)
			{
				((blocos.BlockBit) a).value = this.value;
				this.right = a;
			}
		}	
	}
	
	void attachLeft (Block a ) throws DiferentsBlocks 
	{

		if((type == 0)||(type == 4)||(type == 5))
		{
			if( a instanceof blocos.BlockWire )
			{
				if( 
						(((BlockWire)a).type != 0 ) && 
						(((BlockWire)a).type != 2 ) && 
						(((BlockWire)a).type != 3 )
					)
				{
					throw new DiferentsBlocks();
				}
			}
			if(a instanceof blocos.BlockLogic)
			{
				this.left = a;
			}
			if (a instanceof blocos.BlockBit)
			{				
				this.value = ((blocos.BlockBit) a).value;
				this.left = a;
			}
			if ( a instanceof blocos.BlockSensor)
			{
				this.value = ((blocos.BlockSensor) a).value;
				this.left = a;
			}
		}		
	}

	void attachTop (Block a ) throws DiferentsBlocks 
	{
		
		if((type == 1)||(type == 2))
		{
			if( a instanceof blocos.BlockWire )
			{
				if( 
						(((BlockWire)a).type != 1 ) && 
						(((BlockWire)a).type != 5 )
					)
				{
					throw new DiferentsBlocks();
				}
			}
			if (a instanceof blocos.BlockBit)
			{				
				this.value = ((blocos.BlockBit) a).value;
				this.top = a;
			}
		}
		else if(type == 4)
		{
			if( a instanceof blocos.BlockWire )
			{
				if( 
						(((BlockWire)a).type != 3 )
					)
				{
					throw new DiferentsBlocks();
				}
			}
			if(a instanceof blocos.BlockLogic)
			{
				this.top = a;
			}
			else if (a instanceof blocos.BlockBit)
			{
				((blocos.BlockBit) a).value = this.value;
				this.top = a;
			}
		}
	}
	
	void attachBottom (Block a ) throws DiferentsBlocks 
	{

		
		if((type == 1)||(type == 5))
		{
			if( a instanceof blocos.BlockWire )
			{
				if( 
						(((BlockWire)a).type != 1 ) && 
						(((BlockWire)a).type != 2 )
					)
				{
					throw new DiferentsBlocks();
				}
			}
			if(a instanceof blocos.BlockLogic)
			{
				this.bottom = a;
			}
			if (a instanceof blocos.BlockBit)
			{				
				((blocos.BlockBit) a).value = this.value;
				this.bottom = a;
			}
		}
		else if(type == 3)
		{
			if( a instanceof blocos.BlockWire )
			{
				if( 
						(((BlockWire)a).type != 4 )
					)
				{
					throw new DiferentsBlocks();
				}
			}
			if (a instanceof blocos.BlockBit)
			{				
				this.value = ((blocos.BlockBit) a).value;
				this.bottom = a;
			}
		}
	}
	
}
